import {Result} from '@/base/Result';
import {request} from '@/base/Request';
import {Page} from '@/base/Page';
import {InfoSort} from '@/base/model/InfoSort';

export function getInfoSortList(params: {
  pageIndex: number,
  pageSize: number,
  sortDirection?: string,
  sortProperties?: Array<string>
}): Promise<Result<Page<InfoSort>>> {
  let sortPropertiesString = '';
  if (params.sortProperties && params.sortProperties.length > 0) {
    sortPropertiesString = params.sortProperties.map(x => 'sortProperties=' + x).join('&');
    delete params.sortProperties;
  }
  return request.getPage(InfoSort, '/api/info-sort?' + sortPropertiesString, {params});
}

export function getInfoSort(id: number): Promise<Result<InfoSort>> {
  return request.get(InfoSort, `/api/info-sort/${id}`);
}

export function putInfoSort(id: number, params: {
  name: string
}): Promise<Result<InfoSort>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(InfoSort, `/api/info-sort/${id}`, formData);
}

export function putInfoSortSort(params: {
  id: number[]
}): Promise<Result<InfoSort>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(InfoSort, '/api/info-sort/sort', formData);
}

export function postInfoSort(params: {
  name: string
}): Promise<Result<InfoSort>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(InfoSort, '/api/info-sort', formData);
}

export function deleteInfoSort(id: number): Promise<Result<void>> {
  return request.delete(`/api/info-sort/${id}`);
}
